
package financedata.historicalquotes;

import android.util.Log;
import com.yuanmei.stockeyes.artifacts.Extensions;

import org.json.JSONArray;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class YuanMeiRequest implements IHistoryRequest {
    public final static String LOG_TAG = "YuanMeiRequest";
    private final String   mStockSymbol;
    private final Calendar mStart;
    private final Calendar mEnd;
    private final String   mHistoryInterval;

    private final static SimpleDateFormat sYuanmeiDateFmt;
    static {
        sYuanmeiDateFmt = new SimpleDateFormat("yyyyMMdd");
    }

//    private static final String YUANMEI_BASE_URL
//            = "http://45.78.36.245:8080/index.php/Stock";

    // Use YunSunny Virtual host server
    private static final String YUANMEI_BASE_URL
            = "http://120.24.182.59:13168/index.php/Stock";

    public static final Calendar DEFAULT_FROM = Calendar.getInstance();

    static {
        DEFAULT_FROM.add(Calendar.YEAR, -1);
    }

    public static final Calendar DEFAULT_TO = Calendar.getInstance();
    public static final Interval DEFAULT_INTERVAL = Interval.MONTHLY;

    YuanMeiRequest(String symbol) {
        this(symbol, DEFAULT_INTERVAL);
    }

    private String convertIntervalToString(Interval interval) {
        if (Interval.DAILY == interval) {
            return "DayLine";
        } else if (Interval.WEEKLY == interval) {
            return "WeekLine";
        }
        return "MonthLine";
    }

    YuanMeiRequest(String symbol, Interval interval) {
        mStockSymbol = symbol;
        mHistoryInterval = convertIntervalToString(interval);

        this.mStart = DEFAULT_FROM;
        this.mEnd = DEFAULT_TO;
    }

    YuanMeiRequest(String symbol, Calendar start, Calendar end) {
        this(symbol, start, end, DEFAULT_INTERVAL);
    }

    YuanMeiRequest(String symbol, Calendar start, Calendar end, Interval interval) {
        mStockSymbol      = symbol;
        mStart             =  start;
        mEnd               = end;
        mHistoryInterval = convertIntervalToString(interval);
    }

    YuanMeiRequest(String symbol, Date start, Date end) {
        this(symbol, start, end, DEFAULT_INTERVAL);
    }

    YuanMeiRequest(String symbol, Date start, Date end, Interval interval) {
        this(symbol, interval);
        mStart.setTime(start);
        mEnd.setTime(end);
    }

    private Calendar parseYuanmeiHistoryDate(int date) {
        Calendar c = Calendar.getInstance();
        //yuanmei int date 20160108
        int year  = date / 10000;
        int month = (date % 10000)/100 - 1;
        int day   = (date % 10000) % 100;
        c.set(year, month, day);
        return c;
    }

    public List<HistoricalQuote> getResult() {
        try {

            String req_url = YUANMEI_BASE_URL + "/" + mHistoryInterval
                    + "/StockCode/" + mStockSymbol
                    + "/Start/" + sYuanmeiDateFmt.format(mStart.getTime())
                    + "/End/"   + sYuanmeiDateFmt.format(mEnd.getTime());
            String response = Extensions.getJSONFromUrl(req_url, 300 * 1000);
            JSONArray json_arrays = new JSONArray(response);
            //  date   , open,high,low, close, volume, hand
            //[20160108,1200,1203,1090,1112,3033809968,266476294]
            BigDecimal one_hundred = new BigDecimal("100");
            List<HistoricalQuote> result = new ArrayList<HistoricalQuote>();
            for (int i = 0; i < json_arrays.length(); i++) {
                JSONArray entry       = new JSONArray(json_arrays.getString(i));
                HistoricalQuote quote = new HistoricalQuote(
                            mStockSymbol, parseYuanmeiHistoryDate(entry.getInt(0)),
                            new BigDecimal(entry.getInt(1)).divide(one_hundred, 2, BigDecimal.ROUND_HALF_EVEN),
                            new BigDecimal(entry.getInt(2)).divide(one_hundred, 2, BigDecimal.ROUND_HALF_EVEN),
                            new BigDecimal(entry.getInt(3)).divide(one_hundred, 2, BigDecimal.ROUND_HALF_EVEN),
                            new BigDecimal(entry.getInt(4)).divide(one_hundred, 2, BigDecimal.ROUND_HALF_EVEN),
                            new BigDecimal(entry.getInt(4)).divide(one_hundred, 2, BigDecimal.ROUND_HALF_EVEN),
                            entry.getLong(5)
                        );
                result.add(quote);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "Error: get " + mStockSymbol + " from "
                    + YUANMEI_BASE_URL + " history code fail" ,  e);
            return null;
        }
    }


}
